package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkBlankFillsQuesController.RespModel;
import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkBlankFillsQuesController.SelectPeople;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalyseBlankFillsQuesService;

@Service
public class HomeworkInfoAnalyseBlankFillsQuesServiceImpl implements HomeworkInfoAnalyseBlankFillsQuesService {

    @Autowired
    ElasticQuesService elasticQuesService;

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Override
    public List<RespModel> convertRespModel(List<HomeworkTaskQues> homeworkTaskQueslist,
            List<HomeworkTaskReportQues> homeworkTaskReportQueslist, String taskId, String classId) {
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // ##
        // 获取试题Id
        List<String> quesIds = new ArrayList<>();
        homeworkTaskQueslist.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        List<ElasticYxyYxyQuesResource> questions = elasticQuesService.findByQuesIdIn(quesIds);
        Map<String, ElasticYxyYxyQuesResource> questionMap = new HashMap<String, ElasticYxyYxyQuesResource>();
        questions.forEach(question -> {
            questionMap.put(question.getId(), question);
        });
        // 获取参与本次作业学生人数
        Integer peopleTotal = homeworkTaskReportRepository.countByClassIdAndTaskId(classId, taskId);
        // 组装数据
        homeworkTaskQueslist.forEach(homeworkTaskQues -> {
            RespModel respModel = new RespModel();
            respModel.order = homeworkTaskQues.getOrder();
            respModel.quesId = homeworkTaskQues.getQuesId();
            respModel.accuracy = 0d;
            respModel.quesId = homeworkTaskQues.getQuesId();
            respModel.quesBody = null;
            try {
                respModel.quesBody = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            Integer factTotal = 0;
            List<SelectPeople> selectPeoples = new ArrayList<SelectPeople>();
            List<List<String>> students = new ArrayList<>();
            // 本题答对人数
            Double exactnessTotal1 = 0d;
            // 本题作答人数
            Integer total1 = 0;
            // ##
            List<Integer> optionIndexs = this.optionIndexs();
            for (int i = 0; i < optionIndexs.size(); i++) {
                // ##
                SelectPeople selectPeople = new SelectPeople();
                List<String> studentlist = new ArrayList<String>();
                int total = 0;
                // ##
                for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQueslist) {
                    if (homeworkTaskReportQues.getQuesId().equals(homeworkTaskQues.getQuesId())) {
                        // ##
                        Integer mark = homeworkTaskReportQues.getMark();
                        if (mark == optionIndexs.get(i)) {
                            if (optionIndexs.get(i) != null) {
                                factTotal = factTotal + optionIndexs.get(i);
                            }
                            total++;
                            studentlist.add(homeworkTaskReportQues.getStudentName());
                        }
                    }
                }
                // ##
                selectPeople.mark = optionIndexs.get(i);
                selectPeople.total = total;
                // ##
                selectPeoples.add(selectPeople);
                students.add(studentlist);
            }
            // 本题班级正确率统计
            for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQueslist) {
                if (homeworkTaskReportQues.getQuesId().equals(homeworkTaskQues.getQuesId())) {
                    // ##
                    total1++;
                    if (homeworkTaskReportQues.getMark() != null && homeworkTaskReportQues.getMark() > 0) {
                        exactnessTotal1 = exactnessTotal1 + homeworkTaskReportQues.getMark() * 0.2;
                    }
                }
            }
            respModel.dueTotal = (peopleTotal != null && peopleTotal > 0) ? peopleTotal * 5 : 0;
            respModel.factTotal = factTotal;
            respModel.selectPeoples = selectPeoples;
            respModel.students = students;
            respModel.accuracy = getDouble(exactnessTotal1, total1);
            respModels.add(respModel);
        });

        if (respModels.size() == 0) { return new ArrayList<>(); }

        return respModels;
    }

    /** 得分循环 */
    public List<Integer> optionIndexs() {
        List<Integer> optionIndexs = new ArrayList<>();
        optionIndexs.add(5);
        optionIndexs.add(4);
        optionIndexs.add(3);
        optionIndexs.add(2);
        optionIndexs.add(1);
        optionIndexs.add(0);
        optionIndexs.add(null);
        return optionIndexs;
    }

    // 格式转化
    private Double getDouble(Double exactnessTotal1, Integer total1) {
        if (total1 == 0) { return 0d; }
        if (exactnessTotal1 <= 0) { return 0d; }
        Double x1 = (exactnessTotal1.doubleValue() / total1.doubleValue()) * 100d;
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        double parse = Double.parseDouble(df.format(x1));
        return parse;
    }
}
